﻿var pagesComm = {
    siteLocalize : function (){
       var o = $('#site-localize');
       var menu = $('#site-localization-menu');
	   o.click(function (e){
	       menu.slideToggle("normal");
		  return false;
	   })
       $(document).click(function (event){
		 var target = $(event.target);
		 if(target.attr('id') == 'site-localization-menu' || target.parent().parent().parent().attr('id') == 'site-localization-menu'){
		    return;
		 }
		 else{
		    menu.slideUp("normal");
		 }
       })
    },
    search : function (){
       var searchForm = document.getElementById("search_form"), 
		   input = document.getElementById("search_input"),
           inputValue = input.value,
           submitSite = document.getElementById("search_site"),
	       str = null;

       input.onfocus = input.onclick = function (){
		   if(this.value == inputValue){
			   this.value = '';
               this.style.backgroundImage = 'none';
			}
       };
       input.onkeyup = function (){
            str = encodeURI(this.value.trim());
       };
       searchForm.onsubmit = function (e){
		    if(str){
               this.action = 'http://search.microsoft.com/results.aspx?mkt=zh-cn&setlang=zh-cn&q=' + str + '&form=MSSBCUS'
			}
			else{
			   stopDefault ( e )
			};
       }
       submitSite.onclick = function (e){
		   if(str){
               searchForm.action = 'http://search.microsoft.com/results.aspx?mkt=zh-cn&setlang=zh-cn&q=' + str + '&form=MSSBCUS';
		   }
			else{
			   stopDefault ( e )
			};
       };
       input.onblur = function (){
		   if(this.value.trim().length <= 1){
             this.value = inputValue;
			 this.style.backgroundImage = 'url(http://i3.microsoft.com/en-us/office365/shared/templates/components/o365SearchBar/images/logo_inside.gif)';
		   }
       };
    },
	pageNav : function (){
	  var Menus = $('#nav a');
	  var topMenu = $('#nav>li');
	  var topMenuA = $('#nav>li>a');
	  var current = null;
	  var bodyId = $('body').attr('id');
	  Menus.each(function (){
		  if($(this).attr('id') == bodyId + '_nav'){
			current = $(this);
			$(this).addClass('active');
			findParentNav($(this));
		  }
	  });
	  /*
	  topMenuA.focus(function (){
		 var _this = $(this);
	     _this.next().show();
		 _this.addClass('here');
		 _this.next().find('a:last').blur(function (){
		    _this.removeClass('here');
			_this.next().hide();
		 })
	  })
	  */
      topMenu.hover(function (){
          if(!$(this).find('a').eq(0).hasClass('active')){
		     $(this).find('a').eq(0).addClass('here')
		  }
		  $(this).find('ul').show();
      },function (){
           $(this).find('a').eq(0).removeClass('here');
		   $(this).find('ul').hide();
      })
	  function findParentNav(o){
	      o.parents('.menu-list').prev().addClass('active');
	  }
	}
}

String.prototype.trim = function() { 
  return this.replace(/(\s*)|(\s*$)/g, ""); 
};

function stopDefault ( e ) {
	if ( e && e.preventDefault )
		e.preventDefault();
	else
		window.event.returnValue = false;
	return false;
};

$(function (){
  pagesComm.siteLocalize();
  pagesComm.search();
  pagesComm.pageNav();
})